/***************************************************************************
 创建者: hualei
 开始时间: 2018-12-20
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018-10-15 类的具体实现代码编写

 ***************************************************************************
 *  ROLLER 滚筒指令具体实现
 ***************************************************************************/
#ifndef ROLLERINSTRUCTIONLOGIC_H
#define ROLLERINSTRUCTIONLOGIC_H

#include "instructionEnum.h"
#include "GeneralDefine.h"
#include "rollerinstruction.h"
#include <QTimer>
#include <QObject>
#include "fleetdefine.h"

class ProgramSet;
class ProgramEngine;
class RobotIo;
class McControllerThread;

//enum E_BLOCK_TYPE
//{
//    E_BLOCK_TYPE_NOP,//不使用电动阻挡
//    E_BLOCK_TYPE_MAGANETIC,//给一个do　０或１信号就可以
//    E_BLOCK_TYPE_DC_DRIVER,//一个do控制的电机
//    E_BLOCK_TYPE_HEAVY_MOTOR,//2个do控制电机正反转
//};

//enum E_ROLLER_STATUS
//{
//    E_ROLLER_STATUS_EMPTY,
//    E_ROLLER_STATUS_LOADED,
//    E_ROLLER_STATUS_LIN,
//    E_ROLLER_STATUS_LOUT,
//    E_ROLLER_STATUS_RIN,
//    E_ROLLER_STATUS_ROUT,
//    E_ROLLER_STATUS_ERROR,
//};

//struct RollerMechanismConfig
//{
//    int rollerMechanicalType;//滚筒机构类型。０：超时未到达报错　　１:超时跳转不报错
//    int commonLeftSensor_index;//从０开始
//    int commonRightSensor_index;
//    int commonMotorPositiveDo_index;
//    int commonMotorReverseDo_index;////从０开始
//    int leftTopSensor_index;
//    int leftButtomSensor_index;
//    int rightTopSensor_index;
//    int rightButtomSensor_index;
//    int leftBlockDo1_index;
//    int leftBlockDo2_index;
//    int rightBlockDo1_index;
//    int rightBlockDo2_index;
//    E_BLOCK_TYPE leftBlockType;
//    E_BLOCK_TYPE rightBlockType;
//    int blockWaitTime;
//};

enum E_ROLLER_CONTROL
{
    E_ROLLER_CONTROL_STOP,
    E_ROLLER_CONTROL_POSITIVE,
    E_ROLLER_CONTROL_NEGATIVE,
};

class LiftMechanismInstructionLogic;

class RollerInstructionLogic
{
public:
    RollerInstructionLogic(RobotIo* ioControllerIn, int robotIdIn, ProgramSet* programSetIn,
                           ProgramEngine* programEngineIn,McControllerThread *programMcControllerIn,
                           LiftMechanismInstructionLogic *liftLogicIn);

    int stop();
    int clearAllMsg( );

    E_ROLLER_STATUS getRollerMechanismStatus();
    int setRollerMechanismConfig(RollerMechanismConfig configIn);
    int getRollerMechanismConfig(RollerMechanismConfig &configOut);
    /**
     * @brief startRoller           开始执行滚筒指令
     * @param programNumIn          程序编号
     * @param instructionIn         滚筒指令
     * @param lineIndex             当前执行行
     * @return                      执行状态返回
     */
    E_PROGRAM_LOOP_RESULT startRoller(int programNumIn,const RollerInstruction& instructionIn, int lineIndex);
    E_PROGRAM_LOOP_RESULT roller_run(const RollerInstruction& instructionIn);
    E_PROGRAM_LOOP_RESULT roller_run(const RollerParameter& paraIn);

private:
    void judgeCurrentStatus();
    int writeConfigFile(QString fileName,RollerMechanismConfig configIn);
    /**
     * @brief readConfigFile        读取并初始化设备配置文件
     * @param filePath              设备配置文件路径
     * @return                      初始化状态
     */
    int readConfigFile(const QString &fileName);

    /**
     * @brief isInTop               检测是否在左侧
     * @param typeIn                滚筒机构类型
     * @return                      检测结果
     */
    bool isInCargoFinished(EM_ROLLER_DIRECTION_TYPE cargoMethod);

    /**
     * @brief realTimeSleep         真实时间睡眠
     * @param umicoroSecond         睡眠时间(unit:ms)
     */
    void realTimeSleep(long long umicoroSecond);//最大定时睡去３Ｓ，超过就溢。

    int commonRollerRun(E_ROLLER_CONTROL control,bool isExchangeDirectionIn);
//    E_PROGRAM_LOOP_RESULT blockMechanismUpDown(EM_LIFT_TYPE moveTypeIn, E_BLOCK_TYPE blokType, int motorDo1Index,
//                             int motorDo2Index, int topArrivalIndex, int buttomArrivalIndex, int middleArrivalIndex,
//                     E_BLOCK_TYPE blokTypeB, int motorDo1IndexB,
//                    int motorDo2IndexB, int topArrivalIndexB, int buttomArrivalIndexB,
//                             int middleArrivalIndexB, double blockWaitTime,bool isExchangeDirectionIn=false);
//    void liftUp(E_BLOCK_TYPE blokType, int motorDo1Index,int motorDo2Index,bool isExchangeDirectionIn);
//    void liftDown(E_BLOCK_TYPE blokType, int motorDo1Index,int motorDo2Index,bool isExchangeDirectionIn);
//    void liftStop(E_BLOCK_TYPE blokType, int motorDo1Index,int motorDo2Index,bool isExchangeDirectionIn);

    bool isInTop_block(int topArrivalIndex,int buttomArrivalIndex);
    bool isInButtom_block(int topArrivalIndex,int buttomArrivalIndex);
    bool isInMiddle_block(int middleArrivalIndex,int buttomArrivalIndex,int topArrivalIndex);

private:

    void addMsg( int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0,
                 QString message= "");

private:
//    int rollerMotorType;
    QString configFilePath;
    RollerMechanismConfig mechanismConfig;
    E_ROLLER_STATUS basicStatus;

    /**
     * @brief timer1            定时器
     */
    QTimer timer;

    /**
     * @brief rollerTimer       滚筒定时器
     */
    QTimer  rollerTimer;

    /**
     * @brief programNum        程序编号
     */
    int programNum;

    /**
     * @brief jmpLbNum          异常跳转标签号
     */
    int jmpLbNum;

    /**
     * @brief mcController       算法控制类
     */
    McControllerThread *mcController;

    /**
     * @brief ioControllerIn    io控制对象
     */
    RobotIo *ioController;

    /**
     * @brief robotId           机器人编号
     */
    int robotId;

    /**
     * @brief programSet        指令程序集合
     */
    ProgramSet *programSet;

    /**
     * @brief programEngine     程序解释引擎
     */
    ProgramEngine *programEngine;

    /**
     * @brief isInitialStatus   初始化状态
     */
    bool isInitialStatus;

    LiftMechanismInstructionLogic *liftLogic;

    bool isRuned;

//    int rollerMechanicalType;//滚筒机构类型。０：超时报错　　１:2个do控制正反转的电机，没有阻挡气缸，２个限位检测开关.超时不报错
//    int heavyRollerDriverType;//滚动驱动类型　１　开观点控制　２网络驱动器速度控制
//    double heavyRollerSpeed;//网络驱动器滚筒速度
//    int heavyRoller_axisId;//网络驱动器轴号

//    int commonLeftSensor_index;//从０开始
//    int commonRightSensor_index;
//    int commonMotorPositiveDo_index;
//    int commonMotorReverseDo_index;////从０开始

//    int heavyLeftSensor_index;//从０开始
//    int heavyRightSensor_index;
//    int heavyMotorPositiveDo_index;
//    int heavyMotorReverseDo_index;////从０开始

//    int leftTopSensor_index;
//    int leftButtomSensor_index;
//    int rightTopSensor_index;
//    int rightButtomSensor_index;

//    int leftBlockDo_index;
//    int rightBlockDo_index;
//    E_BLOCK_TYPE leftBlockType;
//    E_BLOCK_TYPE rightBlockType;
//    int blockWaitTime;

};

#endif // ROLLERINSTRUCTIONLOGIC_H
